import socket
from lib.config.index import  wifi_config

s = None

def connect(domain, port):
    global s
    if not s:
        addr = socket.getaddrinfo(domain, port)[0][-1]
        s = socket.socket()
        s.connect(addr)
    else:
        if wifi_config.is_debug():
            print("esp32 tcp: socket is connected")
    return s

def read(size):
    global s
    data = s.recv(size)
    return data

def write(buf):
    global s
    req = f"{buf}"
    s.send(req.encode("ascii"))
    return s

def disconnect():
    global s
    s.close()
    return s